package org.smr.ministore.micro;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.blade.dao.base.BaseMapper;
import org.blade.utils.IdGenUtils;
import org.smr.ministore.dao.DictionaryOrganMapper;
import org.smr.ministore.entities.DictionaryOrgan;
import org.smr.ministore.service.DictionaryOrganService;
import org.blade.service.base.AbstractService;

import org.blade.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class DictionaryOrganMicroService extends AbstractService<Long, DictionaryOrgan> implements DictionaryOrganService {

	@Autowired
	private DictionaryOrganMapper mapper;

	private static final int ZERO = 0;

    public DictionaryOrganMicroService(DictionaryOrganMapper mapper) {
        super(mapper);
    }

    @ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/DictionaryOrgan/batchSave")
	public int batchSave(@RequestBody List<DictionaryOrgan> entities) {
        this.batchSaveAttatch(entities);
    	return mapper.batchInsert(entities);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/DictionaryOrgan/batchModify")
    public int batchModify(List<DictionaryOrgan> entities) {
	    this.batchModifyAttatch(entities);
        return mapper.batchUpdate(entities);
    }


    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT, value = "/DictionaryOrgan/save")
    public int save(@RequestBody DictionaryOrgan entity) {
        this.saveAttatch(entity);
    	return mapper.insert(entity);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.PATCH, value = "/DictionaryOrgan/updateById")
    public int updateById(@RequestBody DictionaryOrgan id) {
        this.modifyAttach(id);
   	 	return mapper.updateById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/DictionaryOrgan/queryById/{id}")
    public DictionaryOrgan queryById( @PathVariable("id")Long id ) {
    	return (DictionaryOrgan) mapper.selectById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/DictionaryOrgan/deleteById/{id}")
    public int deleteById( @PathVariable("id")Long id ) {
        return mapper.deleteById(id);
    }

    @Override
    public int deleteByIdLogical(Long id) {
        return 0;
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/DictionaryOrgan/paginate/{pageSize}/{pageNum}")
    public Pager<DictionaryOrgan> paginate(@RequestBody DictionaryOrgan entity, @PathVariable("pageSize")Long pageSize,  @PathVariable("pageNum")Long pageNum ) {
        Pager<DictionaryOrgan> pager = new Pager(pageSize, pageNum);
        List<DictionaryOrgan> list = this.mapper.pagination( entity, pager );
        pager.setEntities(list);
        return pager;
    }


    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/DictionaryOrgan/assignedDictionary/{organId}")
    public int assignedDictionary(@RequestBody List<DictionaryOrgan> dictionaryOrganList,
                                  @PathVariable("organId") Long organId) {

	    int effectCount = 0;
        effectCount = this.mapper.deleteByOrgan(organId);

        List<DictionaryOrgan> dictionaryOrgansForAdd = new ArrayList<DictionaryOrgan>();
        List<DictionaryOrgan> dictionaryOrgansForUpdate = new ArrayList<DictionaryOrgan>();

        for( DictionaryOrgan dictionaryOrgan : dictionaryOrganList ){

            if(ObjectUtils.notEqual(dictionaryOrgan.getId(), null)){
                dictionaryOrgansForUpdate.add(dictionaryOrgan);
            }else{
                dictionaryOrgansForAdd.add(dictionaryOrgan);

            }
        }

        if( dictionaryOrgansForAdd.size() > ZERO ){

            effectCount += this.batchSave(dictionaryOrgansForAdd);
        }

        if(dictionaryOrgansForUpdate.size() > ZERO){
            effectCount += this.batchModify(dictionaryOrgansForUpdate);

        }

        return effectCount;
    }

    @Override
    public Long getId() {
        return IdGenUtils.getNextLongId();
    }
}
